home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 3794 / 3794.xpi / chrome / content / sidebar.js < prev    next >
Encoding:
JavaScript  |  2009-11-16  |  12.2 KB  |  311 lines

  1. /**
  2.  *
  3.  * The source code included in this file is licensed to you by Facebook under
  4.  * the Apache License, Version 2.0.  Accordingly, the following notice
  5.  * applies to the source code included in this file:
  6.  *
  7.  * Copyright ┬⌐ 2009 Facebook, Inc.
  8.  *
  9.  * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10.  * not use this file except in compliance with the License. You may obtain
  11.  * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16.  * License for the specific language governing permissions and limitations
  17.  * under the License.
  18.  *
  19.  */
  20.  
  21.  
  22. var Cc = Components.classes;
  23. var Ci = Components.interfaces;
  24.  
  25. var fbSvc = Cc['@facebook.com/facebook-service;1'].getService(Ci.fbIFacebookService);
  26. var obsSvc = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
  27. var fbStringBundle = null;
  28.  
  29. var observer = {
  30.     observe: function(subject, topic, data) {
  31.         debug('OBSERVING SOMETHING: ', topic);
  32.         var panel = document.getElementById('facebook-panel');
  33.         topicSwitch:
  34.         switch (topic) {
  35.             case 'facebook-session-end':
  36.                 ClearFriends(true);
  37.                 break;
  38.             case 'facebook-friends-updated':
  39.                 UpdateFriends();
  40.                 break;
  41.             case 'facebook-friend-updated':
  42.                 if( data == 'status-delete' ) {
  43.                   subject = subject.QueryInterface(Ci.fbIFacebookUser);
  44.                   SetStatus(document.getElementById('sidebar-' + subject.id), null, 0);
  45.                 }
  46.                 else if(  data == 'status'
  47.                        || data == 'profile' ) {
  48.                   subject = subject.QueryInterface(Ci.fbIFacebookUser);
  49.                   var elt = document.getElementById('sidebar-' + subject.id);
  50.                   var selSort = document.getElementById('fbSidebarSorter').getAttribute('selectedsort');
  51.                   if( data == 'status' ) {
  52.                     SetStatus( elt, subject.status, subject.stime);
  53.                     if( selSort == 'name' || selSort == 'profile' )
  54.                       break;
  55.                   }
  56.                   else {
  57.                     SetProfileTime(elt, subject.ptime);
  58.                     if( selSort == 'name' || selSort == 'status' )
  59.                       break;
  60.                   }
  61.                   friendsToUpdate.push(subject);
  62.                 }
  63.                 else
  64.                   debug( 'ignoring', topic, data );
  65.                 break;
  66.             case 'facebook-new-friend':
  67.                 friendsToUpdate.push(subject.QueryInterface(Ci.fbIFacebookUser));
  68.                 break;
  69.             case 'facebook-new-day':
  70.                 ClearFriends(false);
  71.                 LoadFriends();
  72.                 break;
  73.         }
  74.     }
  75. };
  76.  
  77. function NameCmp(friend1,friend2) {
  78.   var n1 = friend1.name.toLowerCase();
  79.   var n2 = friend2.name.toLowerCase();
  80.   if (n1 < n2) return -1;
  81.   else if (n1 > n2) return 1;
  82.   else return 0;
  83. };
  84.  
  85. /*
  86.  * Class FriendSort
  87.  * Encapsulates a sort order for a list of facebook friends.
  88.  */
  89. function FriendSort( field, eltId, func){
  90.   debug("Constructor", typeof func);
  91.   this.field = field;
  92.   this.eltId = eltId;
  93.   if( 'function' == typeof func ) {
  94.     var fallback = this.defaultSortFunc;
  95.     this.sortFunc = function(friend1,friend2) {
  96.       var res = func(friend1,friend2);
  97.       return ( 0 != res ) ? res : fallback(friend1,friend2);
  98.     };
  99.   }
  100.   else
  101.     this.sortFunc = this.defaultSortFunc;
  102. }
  103. FriendSort.prototype.__defineGetter__( "label", function() {
  104.     var fld = (this.field == "last update") ? "lastupdate" : this.field;
  105.     var sortie = fbStringBundle.getString(fld);
  106.     return fbStringBundle.getFormattedString('sortingby', [sortie]);
  107. });
  108. FriendSort.prototype.callbackSortFunc =
  109. FriendSort.prototype.defaultSortFunc = NameCmp;
  110.  
  111. var _friend_sorts = {
  112.   'name':   new FriendSort( 'name', 'fbSortName', null ),
  113.   'status': new FriendSort( 'status', 'fbSortStatus',
  114.     function(friend1, friend2) { // compare status update times
  115.       return friend2.stime - friend1.stime;
  116.     }),
  117.   'profile': new FriendSort( 'profile', 'fbSortProfile',
  118.     function(friend1, friend2) { // compare profile update times
  119.       return friend2.ptime - friend1.ptime;
  120.     }),
  121.   'last update': new FriendSort( 'last update', 'fbSortUpdate',
  122.     function(friend1, friend2) { // compare more recent of status,profile update time
  123.       return Math.max( friend2.ptime, friend2.stime )
  124.            - Math.max( friend1.ptime, friend1.stime );
  125.     }),
  126. };
  127.  
  128. function GetFriendSort() {
  129.   var selSort = document.getElementById('fbSidebarSorter').getAttribute('selectedsort');
  130.   var friendSorter  = _friend_sorts[selSort];
  131.   debug( "FriendSort", selSort, friendSorter );
  132.   return friendSorter;
  133. }
  134.  
  135. function SortBy(selSort) {
  136.     debug( "Sorting by...", selSort)
  137.     var sorter = document.getElementById('fbSidebarSorter');
  138.     sorter.setAttribute('selectedsort', selSort);
  139.     sorter.setAttribute('label', _friend_sorts[selSort].label );
  140.     ClearFriends(false);
  141.     debug( "Sort call to LoadFriends" );
  142.     LoadFriends();
  143. }
  144.  
  145. function ClearFriends(sessionEnded) {
  146.     debug( "ClearFriends" );
  147.     var list = document.getElementById('SidebarFriendsList');
  148.     while (list.firstChild && list.firstChild.id != 'FacebookHint') {
  149.         list.removeChild(list.firstChild);
  150.     }
  151.     if (sessionEnded) {
  152.         SetHint(true, fbStringBundle.getString('loadFriends'), 'FacebookLogin()');
  153.     }
  154. }
  155.  
  156. function LoadFriends() {
  157.     var list = document.getElementById('SidebarFriendsList');
  158.     var count = {};
  159.     var friends = fbSvc.getFriends(count);
  160.     debug('Loading friends', count.value);
  161.     if (!fbSvc.loggedIn) {
  162.         SetHint(true, fbStringBundle.getString('loadFriends'), 'FacebookLogin()');
  163.     } else if (!count.value) {
  164.         SetHint(true, fbStringBundle.getString('loadingfriends'), '');
  165.     } else {
  166.         var friendSort = GetFriendSort();
  167.         debug( "Sorting friends", friendSort.field );
  168.         debug( NameCmp == friendSort.defaultSortFunc );
  169.         friends.sort( friendSort.sortFunc );
  170.         // friends.sort(NameCmp);
  171.  
  172.         var hint = document.getElementById('FacebookHint');
  173.         for each (var friend in friends) {
  174.             CreateFriendNode(list, friend, hint);
  175.         }
  176.         var searchTerm = GetFBSearchBox().value;
  177.         if (searchTerm != fbStringBundle.getString('searchplaceholder')) {
  178.             SearchFriends(searchTerm);
  179.         } else {
  180.             SetHint(false, '', '');
  181.         }
  182.     }
  183. }
  184.  
  185. var friendsToUpdate = [];
  186. function UpdateFriends() {
  187.     debug('UpdateFriends');
  188.     var list = document.getElementById('SidebarFriendsList');
  189.     if (!list.firstChild || list.firstChild.id == 'FacebookHint') {
  190.         return LoadFriends();
  191.     }
  192.     var sorter = GetFriendSort();
  193.     friendsToUpdate.sort(sorter.sortFunc);
  194.     var first = list.firstChild;
  195.     for each (var friend in friendsToUpdate) {
  196.         var toRemove = document.getElementById('sidebar-' + friend.id);
  197.         debug('remove:', toRemove, friend.id, friend.name);
  198.         if (toRemove) {
  199.             if (toRemove == first) {
  200.                 // fixes the problem where the first person in your list updates their status
  201.                 first = first.nextSibling;
  202.             }
  203.             list.removeChild(toRemove);
  204.         }
  205.         CreateFriendNode(list, friend, first);
  206.     }
  207.     friendsToUpdate = [];
  208.     var searchTerm = GetFBSearchBox().value;
  209.     if (searchTerm != fbStringBundle.getString('searchplaceholder')) {
  210.         SearchFriends(searchTerm);
  211.     }
  212. }
  213.  
  214. function CreateFriendNode(list, friend, insertBefore) {
  215.     if (!friend.name) return;
  216.     var item = document.createElement('richlistitem');
  217.     item.setAttribute('id', 'sidebar-' + friend.id);
  218.     item.setAttribute('class', 'friendBox');
  219.     item.setAttribute('friendname', friend.name);
  220.     //item.setAttribute('wall', 'wall: ' + friend.wall);
  221.     //item.setAttribute('notes', 'notes: ' + friend.notes);
  222.     var firstName = friend.name.substr(0, friend.name.indexOf(' '));
  223.     if (!firstName) firstName = friend.name;
  224.     item.setAttribute('firstname', firstName);
  225.     SetStatus(item, friend.status, friend.stime);
  226.     item.setAttribute('ptime', getProfileTime(friend.ptime) );
  227.     item.setAttribute('oncommand', "OpenFBUrl('profile.php', '" + friend.id + "', event, null )");
  228.     item.setAttribute('viewUpdCmd', "OpenFBUrl('profile.php', '" + friend.id + "', event, {highlight: null} ); return false;");
  229.     item.setAttribute('msgCmd', "OpenFBUrl('message.php', '" + friend.id + "', event, null )");
  230.     item.setAttribute('pokeCmd', "OpenFBUrl('poke.php', '" + friend.id + "', event, null )");
  231.     item.setAttribute('postCmd', "OpenFBUrl('wallpost.php', '" + friend.id + "', event, null )");
  232.     //item.setAttribute('wallCmd', "OpenFBUrl('wall.php', '" + friend.id + "', event)");
  233.     //item.setAttribute('notesCmd', "OpenFBUrl('notes.php', '" + friend.id + "', event)");
  234.     item.setAttribute('pic', friend.pic);
  235.     list.insertBefore(item, insertBefore);
  236. }
  237.  
  238. function OpenSettings() { /* modified from optionsmenu extension */
  239.     debug("OpenSettings()");
  240.     var url = "chrome://facebook/content/settings.xul";
  241.     var features = "chrome,titlebar,toolbar,centerscreen";
  242.     try {
  243.         var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
  244.         var instantApply = prefs.getBoolPref("browser.preferences.instantApply");
  245.         features += instantApply ? ",dialog=no" : ",modal";
  246.     }
  247.     catch (e) {
  248.         features += ",modal";
  249.     }
  250.     openDialog( url, "", features);
  251. }
  252.  
  253. var _sidebar_topics = ['facebook-new-friend',
  254.                        'facebook-friend-updated',
  255.                        'facebook-friends-updated',
  256.                        'facebook-session-end',
  257.                        'facebook-new-day' ];
  258. function SidebarLoad() {
  259.     debug('SidebarLoad');
  260.     fbStringBundle = GetFBStringBundle()
  261.     top.document.getElementById('facebook-sidebar-toggle').checked = true;
  262.     top.document.getElementById('PopupFacebookFriends').hidePopup(); // just in case it was still showing
  263.  
  264.     var sorter = document.getElementById('fbSidebarSorter');
  265.     var selSort = sorter.getAttribute('selectedsort');
  266.     var friendSort = _friend_sorts[selSort];
  267.     debug( "selSort", selSort, friendSort.label, friendSort.eltId );
  268.     sorter.setAttribute('label', friendSort.label );
  269.     document.getElementById(friendSort.eltId).setAttribute('checked', 'true');
  270.  
  271.     LoadFriends();
  272.     for each( var topic in _sidebar_topics )
  273.       obsSvc.addObserver(observer, topic, false);
  274.     // document.getElementById('SidebarFriendsList').addEventListener('keypress', HandleKeyPress, true);
  275.     if (!top.document.getElementById('facebook-search')) {
  276.         // XXX for some reason even if the toolbar is hidden we can still see
  277.         // the search-box, so this never happens...we'll keep the code in case
  278.         // we get a chance to figure it out later, though.
  279.         document.getElementById('facebook-search-sidebar').style.display = '';
  280.         document.getElementById('facebook-search-sidebar').addEventListener('keypress', HandleKeyPress, true);
  281.     } else {
  282.         document.getElementById('facebook-search-sidebar').style.display = 'none';
  283.     }
  284.     // top.document.getElementById('sidebar-splitter').addEventListener('mouseup', SidebarResize, false);
  285.     // SidebarResize();
  286. }
  287. function SidebarUnload() {
  288.     debug('SidebarUnload');
  289.     top.document.getElementById('facebook-sidebar-toggle').checked = false;
  290.     for each( var topic in _sidebar_topics )
  291.       obsSvc.removeObserver(observer, topic);
  292.     // top.document.getElementById('sidebar-splitter').removeEventListener('mouseup', SidebarResize, false);
  293. }
  294.  
  295. /*
  296. var statusWidthStyleRule = false;
  297. function SidebarResize() {
  298.     debug('setting status width', window.innerWidth);
  299.     var sheet = document.styleSheets[0];
  300.     if (false !== statusWidthStyleRule) {
  301.         debug('deleting', statusWidthStyleRule, sheet.cssRules.length);
  302.         sheet.deleteRule(statusWidthStyleRule);
  303.     }
  304.     statusWidthStyleRule = sheet.cssRules.length;
  305.     sheet.insertRule(".status { width: " + (window.innerWidth-90) + "px !important; }", statusWidthStyleRule);
  306. }
  307. */
  308. var facebook=null; // for some reason lib.js can't seem to handle not having something named facebook defined
  309.  
  310. debug('loaded sidebar.js');
  311.